package com.zerdoor.controller;

import com.zerdoor.model.UserInfo;
import com.zerdoor.pagehelper.PageHelper;
import com.zerdoor.pagehelper.PageInfo;
import com.zerdoor.service.ItemAppraiseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 评价信息--控制层
 * Created by zhongjieyu on 2015/1/22.
 */
@Controller
@RequestMapping("/appraise")
public class ItemAppraiseController {

    @Autowired
    private ItemAppraiseService itemAppraiseService;

    /**
     * 跳转评价管理页
     * @param request
     * @return
     */
    @RequestMapping(value = "index", method = RequestMethod.GET)
    public String index(HttpServletRequest request){
        return "/appraise/view";
    }

    /**
     * 查询评价信息
     * @param request
     * @return
     */
    @RequestMapping(value = "/view", method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> appraise(HttpServletRequest request,int page,int rows){

        Map<String, Object> result = new HashMap<String, Object>();
        PageHelper.startPage(page, rows);
        result.put("contract_id",request.getParameter("contract_id"));
        result.put("by_appraiser",request.getParameter("by_appraiser"));
        result.put("appraiser",request.getParameter("appraiser"));
        List<Map<String, Object>> list = null;
        try {
            list = itemAppraiseService.findItemAppraise(result);

        } catch (Exception e) {
            e.printStackTrace();

            result.put("errormsg","查询失败！");
            return result;
        }
        PageInfo pageInfo = new PageInfo(list);
        result.put("rows", list);
        result.put("total", pageInfo.getTotal());
        return result;
    }

    /**
     * 逻辑删除评价信息
     * @param request
     * @param appraiseId ID
     * @return
     */
    @RequestMapping(value = "/deleteAppraise", method = RequestMethod.POST)
    public ResponseEntity<?> deleteAppraise(HttpServletRequest request,int appraiseId){

//        //获取当前用户ID
//        UserInfo userInfo = (UserInfo)request.getSession().getAttribute("curUser");

        try {
            itemAppraiseService.deleteAppraise(appraiseId);
        } catch (Exception e) {
            e.printStackTrace();

            ResponseEntity<String> responseEntity = new ResponseEntity<String>(e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
            return responseEntity;
        }



        ResponseEntity<String> responseEntity = new ResponseEntity<String>("success", HttpStatus.OK);
        return responseEntity;

    }

}
